﻿using System;
using Guia.Core;
using Guia.Elements;
using NUnit.Framework;
using WpfTestApplication;

namespace NUnitTests{
    /// <summary>
    /// Summary description for ListGridViewTest
    /// </summary>
    [TestFixture]
    public class ListGridViewTest : AbstractWindowTest
    {
        public ListGridViewTest()
        {
        }

        protected override Type TestWindowType()
        {
            return typeof(ListGridViewTestWindow);
        }

        protected override string TestWindowName()
        {
            return "ListGridViewTestWindow";
        }

        [Test]
        [ExpectedException(typeof(ElementNotFoundException))]
        public void TestNoListView()
        {
            // A Listview can become a DataGrid in sense of UIA, so its no more a ListView.
            ListView listView = window.Get<ListView>("listViewGrid");
            Assert.IsNull(listView);
        }

        [Test]
        public void TestGridHeaderCount()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual(3, listView.Header.ColumnCount);
            Assert.AreEqual(3, listView.Header.HeaderColumns.Length);
        }

        [Test]
        public void TestGridHeaderDirectAccess()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual("Creator", listView.Header["Creator"].Name);
            Assert.AreEqual("Creator", listView.Header[1].Name);
        }

        [Test]
        public void TestGridHeaderAccessAll()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual("Game Name", listView.Header.HeaderColumns[0].Name);
            Assert.AreEqual("Creator", listView.Header.HeaderColumns[1].Name);
            Assert.AreEqual("Publisher", listView.Header.HeaderColumns[2].Name);
        }

        [Test]
        public void TestGridCellDimensions()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual(3,listView.RowCount);
            Assert.AreEqual(3, listView.ColumnCount);
        }
        
        [Test]
        public void TestGridCellAccessRead()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual("World Of Warcraft", listView[0, 0].TextValue);
            Assert.AreEqual("Blizzard", listView[0, 1].TextValue);
            Assert.AreEqual("Blizzard", listView[0, 2].TextValue);
            Assert.AreEqual("Halo", listView[1, 0].TextValue);
            Assert.AreEqual("Bungie", listView[1, 1].TextValue);
            Assert.AreEqual("Microsoft", listView[1, 2].TextValue);
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGridCellOutOfBounds()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual("no cell here", listView[3, 0].Name);
        }

        [Test]
        public void TestGridCellInformation()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            DataGridCell_ListView cell = listView[2, 1];
            Assert.AreEqual(1, cell.Column);
            Assert.AreEqual(2, cell.Row);
            Assert.AreEqual(1, cell.ColumnSpan);
            Assert.AreEqual(1, cell.RowSpan);
        }

        [Test]
        public void TestGridRowsAccess()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual(3, listView.Rows.Length);
        }

        [Test]
        public void TestGridRowsCellsAccessInformation()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual(3, listView.Rows[0].Cells.Length);
            Assert.AreEqual(2, listView.Rows[0].Cells[2].Column);
            Assert.AreEqual(0, listView.Rows[0].Cells[2].Row);
            Assert.AreEqual(1, listView.Rows[0].Cells[2].RowSpan);
            Assert.AreEqual(1, listView.Rows[0].Cells[2].ColumnSpan);
        }

        [Test]
        public void TestGridRowSelectionMethods()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual(false, listView.Rows[0].IsSelected);
            listView.Rows[0].Select();
            Assert.AreEqual(true, listView.Rows[0].IsSelected);
            listView.Rows[0].Unselect();
            Assert.AreEqual(false, listView.Rows[0].IsSelected);
        }

        [Test]
        public void TestGridRowSelectionIsSelected()
        {
            DataGrid_ListView listView = window.Get<DataGrid_ListView>("listViewGrid");
            Assert.IsNotNull(listView);
            Assert.AreEqual(false, listView.Rows[0].IsSelected);
            listView.Rows[0].IsSelected = true;
            Assert.AreEqual(true, listView.Rows[0].IsSelected);
            listView.Rows[0].IsSelected = false;
            Assert.AreEqual(false, listView.Rows[0].IsSelected);
        }
    }
}
